<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="流水号" prop="serialNo">
        <el-input
            v-model="queryParams.serialNo"
            placeholder="请输入流水号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领料单号" prop="materialRequisitionId">
        <el-input
            v-model="queryParams.materialRequisitionId"
            placeholder="请输入领料单号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialId">
        <el-select v-model="queryParams.materialId" placeholder="请选择物料名称" clearable filterable size="small">
          <el-option v-for="item in selectOption.material" :value="item.value" :label="item.label" :key="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="检验人" prop="checkUser">
        <el-select v-model="queryParams.checkUser" placeholder="请选择检验人" clearable filterable size="small">
          <el-option v-for="item in selectOption.checkUser" :value="item.value" :label="item.label" :key="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="检验时间" prop="checkDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.checkDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择检验时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['mes:materialCheck:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['mes:materialCheck:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['mes:materialCheck:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['mes:materialCheck:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialCheckList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="流水号" align="center" prop="serialNo" show-overflow-tooltip/>
      <el-table-column label="领料单号" align="center" prop="materialRequisitionId" show-overflow-tooltip/>
      <el-table-column label="物料名称" align="center" prop="material" show-overflow-tooltip/>
      <el-table-column label="规格" align="center" prop="specification" show-overflow-tooltip/>
      <el-table-column label="型号" align="center" prop="model" show-overflow-tooltip/>
      <el-table-column label="单位" align="center" prop="unit" show-overflow-tooltip/>
      <el-table-column label="需求数量" align="center" prop="requireQuantity" show-overflow-tooltip/>
      <el-table-column label="已领数量" align="center" prop="receiveQuantity" show-overflow-tooltip/>
      <el-table-column label="抽检数量" align="center" prop="checkQuantity" show-overflow-tooltip/>
      <el-table-column label="检验项" align="center" prop="checkStandardId" show-overflow-tooltip/>
      <el-table-column label="标准值上限" align="center" prop="maxValue" show-overflow-tooltip/>
      <el-table-column label="标准值下限" align="center" prop="minValue" show-overflow-tooltip/>
      <el-table-column label="检验值" align="center" prop="checkValue" show-overflow-tooltip/>
      <el-table-column label="检验结果" align="center" prop="checkResult" :formatter="checkResultFormat"
                       show-overflow-tooltip
      />
      <el-table-column label="检验人" align="center" prop="checkUserName" show-overflow-tooltip/>
      <el-table-column label="检验时间" align="center" prop="checkDate" width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['mes:materialCheck:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['mes:materialCheck:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改来料检验对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="领料编号" prop="materialRequisitionId">
          <el-input disabled v-model="form.materialRequisitionId" placeholder="请选择领料编号">
            <el-button @click="selectRequisition" slot="append">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="物料名称" prop="material">
          <el-input disabled v-model="form.material" placeholder="物料名称">
            <el-button @click="selectMaterial" slot="append">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="抽检数量" prop="checkQuantity">
          <el-input v-model="form.checkQuantity" placeholder="请输入抽检数量"/>
        </el-form-item>
        <el-form-item label="检验值" prop="checkValue">
          <el-input v-model="form.checkValue" placeholder="请输入检验值"/>
        </el-form-item>
        <el-form-item label="检验结果" prop="checkResult">
          <el-select v-model="form.checkResult" placeholder="请选择检验结果：0不合格1合格" clearable filterable>
            <el-option
                v-for="dict in checkResultOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <RequisitionSelect @onSelected="onRequisitionSelected" @onCancel="onRequisitionCancel"
                       :open="requisitionSelectOpen"
    ></RequisitionSelect>
    <MaterialSelect @onSelected="onMaterialSelected" @onCancel="onMaterialCancel" :open="materialSelectOpen"
    ></MaterialSelect>
  </div>
</template>

<script>
import {
  addMaterialCheck,
  delMaterialCheck,
  exportMaterialCheck,
  getMaterialCheck,
  listMaterialCheck,
  querySelectOption,
  updateMaterialCheck
} from '@/api/mes/materialCheck'
import { parseTime } from '../../../utils/neu'
import RequisitionSelect from '@/components/product/RequisitionSelect.vue'
import MaterialSelect from '@/components/Material/MaterialSelect.vue'

export default {
  name: 'MaterialCheck',
  components: { MaterialSelect, RequisitionSelect },
  data() {
    return {
      selectOption: {},
      requisitionSelectOpen: false,
      materialSelectOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 来料检验表格数据
      materialCheckList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 检验结果：0不合格1合格字典
      checkResultOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNo: null,
        materialRequisitionId: null,
        materialId: null,
        checkUser: null,
        checkDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialRequisitionId: [
          { required: true, message: '领料单编号不能为空', trigger: 'blur' }
        ],
        material: [
          { required: true, message: '物料名称不能为空', trigger: 'change' }
        ],
        checkQuantity: [
          { required: true, message: '抽检数量不能为空', trigger: 'blur' }
        ],
        checkValue: [
          { required: true, message: '检验值不能为空', trigger: 'blur' }
        ],
        checkResult: [
          { required: true, message: '检验结果不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.querySelectOption()
    this.getDicts('mes_check_result').then(response => {
      this.checkResultOptions = response.data
    })
  },
  methods: {
    querySelectOption() {
      querySelectOption().then(res => {
        this.selectOption = res.data
      })
    },
    onRequisitionCancel() {
      this.requisitionSelectOpen = false
    },
    onRequisitionSelected(row) {
      this.form = {
        ...this.form,
        materialRequisitionId: row.id
      }
      this.requisitionSelectOpen = false
    },
    onMaterialSelected(row) {
      const material = row[0]
      this.form = {
        ...this.form,
        material: material.name,
        materialId: material.id
      }
      this.materialSelectOpen = false
    },
    onMaterialCancel() {
      this.materialSelectOpen = false
    },
    selectRequisition() {
      this.requisitionSelectOpen = true
    },
    selectMaterial() {
      this.materialSelectOpen = true
    },
    parseTime,
    /** 查询来料检验列表 */
    getList() {
      this.loading = true
      listMaterialCheck(this.queryParams).then(response => {
        this.materialCheckList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 检验结果：0不合格1合格字典翻译
    checkResultFormat(row, column) {
      return this.selectDictLabel(this.checkResultOptions, row.checkResult)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serialNo: null,
        materialRequisitionId: null,
        materialRequisitionNo: null,
        materialId: null,
        checkStandardId: null,
        requireQuantity: null,
        receiveQuantity: null,
        checkQuantity: null,
        checkValue: null,
        maxValue: null,
        minValue: null,
        checkResult: null,
        remark: null,
        checkUser: null,
        checkDate: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加来料检验'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getMaterialCheck(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改来料检验'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMaterialCheck(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addMaterialCheck(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除来料检验编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delMaterialCheck(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有来料检验数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportMaterialCheck(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>
